package com.sale.salesystem.service.impl;

import com.sale.salesystem.service.AnalysisService;
import com.sale.salesystem.vo.DashboardVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import com.sale.salesystem.dto.DetailQueryDTO;
import com.sale.salesystem.entity.ReportData;
import com.sale.salesystem.mapper.ReportDataMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@RequiredArgsConstructor
public class AnalysisServiceImpl implements AnalysisService {

    private final ReportDataMapper reportDataMapper;
    private static final Logger log = LoggerFactory.getLogger(AnalysisServiceImpl.class);

    @Override
    public DashboardVO getDashboardData(String type) {
        DashboardVO data = new DashboardVO();
        
        // 根据类型获取不同的仪表板数据
        switch (type) {
            case "core":
                return getCoreAnalysisData();
            case "product":
                return getProductAnalysisData();
            case "customer":
                return getCustomerAnalysisData();
            case "shop":
                return getShopAnalysisData();
            case "income":
                return getIncomeAnalysisData();
            case "detail":
                return getDetailAnalysisData();
            default:
                return data;
        }
    }

    private DashboardVO getCoreAnalysisData() {
        DashboardVO data = new DashboardVO();
        data.setDashboardId(1L);
        data.setDashboardName("核心数据分析");
        data.setType("core");
        // 设置其他数据...
        return data;
    }

    private DashboardVO getProductAnalysisData() {
        DashboardVO data = new DashboardVO();
        data.setDashboardId(2L);
        data.setDashboardName("产品数据分析");
        data.setType("product");
        // 设置其他数据...
        return data;
    }

    private DashboardVO getCustomerAnalysisData() {
        DashboardVO data = new DashboardVO();
        data.setDashboardId(3L);
        data.setDashboardName("客户数据分析");
        data.setType("customer");
        // 设置其他数据...
        return data;
    }

    private DashboardVO getShopAnalysisData() {
        DashboardVO data = new DashboardVO();
        data.setDashboardId(4L);
        data.setDashboardName("商店数据监控");
        data.setType("shop");
        // 设置其他数据...
        return data;
    }

    private DashboardVO getIncomeAnalysisData() {
        DashboardVO data = new DashboardVO();
        data.setDashboardId(5L);
        data.setDashboardName("收入数据分析");
        data.setType("income");
        // 设置其他数据...
        return data;
    }

    private DashboardVO getDetailAnalysisData() {
        DashboardVO data = new DashboardVO();
        data.setDashboardId(6L);
        data.setDashboardName("详细数据展示");
        data.setType("detail");
        // 设置其他数据...
        return data;
    }

    @Override
    public IPage<ReportData> getAllData(DetailQueryDTO queryDTO) {
        log.info("分页参数: pageNum={}, pageSize={}", queryDTO.getPageNum(), queryDTO.getPageSize());
        
        // 创建分页对象
        Page<ReportData> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        
        // 构建查询条件
        LambdaQueryWrapper<ReportData> wrapper = new LambdaQueryWrapper<>();
        
        // 日期范围
        if (queryDTO.getStartDate() != null) {
            wrapper.ge(ReportData::getCreateTime, queryDTO.getStartDate());
        }
        if (queryDTO.getEndDate() != null) {
            wrapper.le(ReportData::getCreateTime, queryDTO.getEndDate());
        }
        
        // 其他条件
        if (StringUtils.hasText(queryDTO.getCategory())) {
            wrapper.eq(ReportData::getType, queryDTO.getCategory());
        }
        if (StringUtils.hasText(queryDTO.getCity())) {
            wrapper.eq(ReportData::getCity, queryDTO.getCity());
        }
        if (queryDTO.getShopId() != null) {
            wrapper.eq(ReportData::getShopCode, queryDTO.getShopId());
        }
        if (StringUtils.hasText(queryDTO.getGender())) {
            wrapper.eq(ReportData::getSex, queryDTO.getGender());
        }
        if (StringUtils.hasText(queryDTO.getAgeRange())) {
            wrapper.eq(ReportData::getAge, queryDTO.getAgeRange());
        }
        
        // 利润条件
        if (queryDTO.getProfitAmount() != null && StringUtils.hasText(queryDTO.getProfitOperator())) {
            switch (queryDTO.getProfitOperator()) {
                case "=" -> wrapper.eq(ReportData::getProfit, queryDTO.getProfitAmount());
                case "<" -> wrapper.lt(ReportData::getProfit, queryDTO.getProfitAmount());
                case "<=" -> wrapper.le(ReportData::getProfit, queryDTO.getProfitAmount());
                case ">" -> wrapper.gt(ReportData::getProfit, queryDTO.getProfitAmount());
                case ">=" -> wrapper.ge(ReportData::getProfit, queryDTO.getProfitAmount());
            }
        }
        
        // 销售额条件
        if (queryDTO.getSalesAmount() != null && StringUtils.hasText(queryDTO.getSalesOperator())) {
            switch (queryDTO.getSalesOperator()) {
                case "=" -> wrapper.eq(ReportData::getPrice, queryDTO.getSalesAmount());
                case "<" -> wrapper.lt(ReportData::getPrice, queryDTO.getSalesAmount());
                case "<=" -> wrapper.le(ReportData::getPrice, queryDTO.getSalesAmount());
                case ">" -> wrapper.gt(ReportData::getPrice, queryDTO.getSalesAmount());
                case ">=" -> wrapper.ge(ReportData::getPrice, queryDTO.getSalesAmount());
            }
        }
        
        // 执行查询
        IPage<ReportData> result = reportDataMapper.selectPage(page, wrapper);
        log.info("查询结果: total={}, pages={}, current={}, size={}", 
            result.getTotal(), result.getPages(), result.getCurrent(), result.getSize());
        
        return result;
    }
} 